package com.qianfeng.vhly.youshare;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.*;
import android.widget.TextView;
import com.qianfeng.vhly.youshare.adapters.CommonFragmentAdapter;
import com.qianfeng.vhly.youshare.fragments.StoreCoreFragment;
import com.qianfeng.vhly.youshare.fragments.StorePlatformFragment;
import com.qianfeng.vhly.youshare.model.Category;

import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 15/2/5
 * Email: vhly@163.com
 */

/**
 * 商城界面Activity
 */
public class StoreActivity extends FragmentActivity implements ActionBar.TabListener, ViewPager.OnPageChangeListener {

    private ViewPager storePager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store);

        // ActionBar 调整样式

        ActionBar actionBar = getActionBar();

        if (actionBar != null) {

            // 设置返回按钮
            actionBar.setDisplayHomeAsUpEnabled(true);

            // 去掉ActionBar上面的图标
            actionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));

            // 标题居中
            actionBar.setDisplayShowCustomEnabled(true);

            // 从layout文件夹中加载布局，然后通过代码修改标题，
            // 实现一个标题 layout布局的共用.
            LayoutInflater inflater = LayoutInflater.from(this);

            View view = inflater.inflate(R.layout.actionbar_main_title, null);

            // 获取view中的 TextView，进行标题的设置

            TextView txtTitle = (TextView) view.findViewById(R.id.actionbar_title);
            if (txtTitle != null) {
                txtTitle.setText("商城");
            }

            actionBar.setCustomView(
                    view,
                    new ActionBar.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            Gravity.CENTER
                    )
            );

            // 取消默认标题
            actionBar.setDisplayShowTitleEnabled(false);

            actionBar.setBackgroundDrawable(new ColorDrawable(0xFFF3603A));

            // 设置ActionBar Tab 导航
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            // ViewPager
            storePager = (ViewPager) findViewById(R.id.store_pager);

            DataStorage dataStorage = DataStorage.getInstance();

            Category firstCategory = dataStorage.getCategory("1");
            Category secondCategory = dataStorage.getCategory("2");

            /// ActionBar 添加 Tab

            ActionBar.Tab tab = actionBar.newTab();

            // 通过从服务器获取的分类名称，来设置界面Tab的标题
            if (firstCategory != null) {
                String name = firstCategory.getName();
                if (name != null) {
                    tab.setText(name);
                } else {
                    tab.setText("核心产品");
                }
            } else {
                tab.setText("核心产品");
            }

            tab.setTag("1");
            // tab 必须要制定 TabListener
            tab.setTabListener(this);

            actionBar.addTab(tab);

            //// 第二个 Tab

            tab = actionBar.newTab();

            // 通过从服务器获取的分类名称，来设置界面Tab的标题
            if (secondCategory != null) {
                String name = secondCategory.getName();
                if (name != null) {
                    tab.setText(name);
                } else {
                    tab.setText("平台产品");
                }
            } else {
                tab.setText("平台产品");
            }

            tab.setTag("2");
            // tab 必须要制定 TabListener
            tab.setTabListener(this);

            actionBar.addTab(tab);

            // 对于ActionBar Tab 与 ViewPager 联动，需要提前创建
            // ViewPager实例，在第一个 addTab 之前创建
            LinkedList<Fragment> fragments = new LinkedList<Fragment>();

            // 添加相应的Fragment
            // 添加核心产品
            fragments.add(new StoreCoreFragment());
            // 添加平台产品
            fragments.add(new StorePlatformFragment());


            CommonFragmentAdapter adapter
                    = new CommonFragmentAdapter(
                    getSupportFragmentManager(),
                    fragments
            );
            storePager.setAdapter(adapter);

            storePager.setOnPageChangeListener(this);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.store, menu);

        return true;
    }

    //////// ActionBar TabListener ///////

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        if (storePager != null) {
            ActionBar actionBar = getActionBar();
            if (actionBar != null) {
                int index = actionBar.getSelectedNavigationIndex();
                storePager.setCurrentItem(index);
            }
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    //////// ViewPager 页面切换进行的事件处理

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setSelectedNavigationItem(i);
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}